package test.hello.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.List;

import test.hello.annotation.MyAnnotation;

public class ReflexUtil {
	
	//自定义注解使用
	public Object Reflex(Object obj) throws Exception{
		Field[] fields =  obj.getClass().getDeclaredFields();
		for(Field field: fields ) {
			field.setAccessible(true);
			Annotation[] ans = field.getAnnotations();
			if (ans !=null && ans.length >0) {
				MyAnnotation an = (MyAnnotation)ans[0];
				field.get(an.str());
				field.get(an.bool());
				field.get(an.index());
			}
			field.get(obj);
		}
		return obj;
	}
	
	//List转成JAVABEAN
	public static Object setJavaBean(Object obj,List<String> list) throws Exception {
		Field[] fieldList = obj.getClass().getDeclaredFields();
		for(int i=0;i<fieldList.length;i++) {
			fieldList[i].setAccessible(true);
			if(fieldList[i].getType() == java.lang.String.class) {
				fieldList[i].set(obj, list.get(i));
			}else if (fieldList[i].getType() == java.lang.Integer.class) {
				fieldList[i].set(obj, Integer.valueOf(list.get(i)));
			}
			
			
		}
		return obj;
		
	}

}
